package tetris.features;

import tetris.simulator.*;

public class featureutils {
	public double[] features(State st, int action)
	{
		//score a state based on simple field features
		//double score;
	
		//maximum height, average height
		
	State s= new State(st);
	s.makeMove(action);
		int[] top = s.getTop();
		int maxHeight = 0;
		int minHeight = State.ROWS;
		double avgHeight = 0;
		for(int i = 0; i < top.length; i++) {
			if(top[i] > maxHeight) {
				maxHeight = top[i];
			}
			if(top[i] < minHeight) {
				minHeight = top[i];
			}
			avgHeight = avgHeight+top[i];
		}
		avgHeight /= top.length;
		int altDiff = maxHeight-minHeight;
		
		int[][] field = s.getField();
		int holes = 0;
		//total filled cells
		int blocks = 0;
		//filled cells weighted by height
		double weightedBlocks = 0;
		for(int c = 0; c < State.COLS; c++) {
			blocks = blocks+top[c]+1;
			weightedBlocks = weightedBlocks+0.5*(top[c]+1)*(top[c]+2);
			for(int r = 0; r < top[c]; r++) {
				if(field[r][c] == 0) {
					holes++;
					blocks--;
					weightedBlocks = weightedBlocks-(r+1);
				}
			}
		}

		//rows removed in previous step
		int rowsCleared = s.getLastRowsCleared();

		//total + deepest wells
		int wells = 0;
		int deepestWell = 0;
		int depth;
		for(int c = 0; c < State.COLS-1; c++) {
			depth = Math.abs(top[c]-top[c+1]);
			wells = wells+depth;
			if(depth > deepestWell) {
				deepestWell = depth;
			}
		}

		// **********************************************************************
		
		//row transitions
		int rowTransitions = 0;
		int flag;
		for(int r = 0; r < s.ROWS; r++){
			flag = 1;
			for(int c = 0; c < s.COLS; c++){
				if(field[r][c] > 0 && flag == 0){
					rowTransitions += 1;
					flag = 1;
				}else if(field[r][c] == 0 && flag == 1){
					rowTransitions += 1;
					flag = 0;
				}else{
					continue;
				}
				}
			if(field[r][s.COLS-1] == 0){
				rowTransitions += 1;
			}
		}
		
		//column transitions
		int colTransitions = 0;
		for(int c = 0; c < s.COLS; c++){
			flag = 1;
			for(int r = 0; r < s.ROWS; r++){
				if(field[r][c] > 0 && flag == 0){
					colTransitions += 1;
					flag = 1;
				}else if(field[r][c] == 0 && flag == 1){
					colTransitions += 1;
					flag = 0;
				}else{
					continue;
				}
				}
			if(field[s.ROWS-1][c] == 0){
				colTransitions += 1;
			}
		}
		

		
		// ****************************************
		//height last piece was placed
		int lastHeight = s.getLastHeightPlaced();
		double[] features = {-maxHeight,-avgHeight, altDiff, -holes, -blocks, -weightedBlocks, rowsCleared, -wells, -deepestWell, -lastHeight, -rowTransitions,-colTransitions};
		//double[] features = {maxHeight, avgHeight, altDiff, holes, blocks, weightedBlocks, rowsCleared, wells, deepestWell,lastHeight,rowTransitions,	colTransitions};
		return features; 
		//return dotProduct(scoreArray, weights);		
	}



public double dotProduct(double[] array1, double[] array2)
{
	double res = 0;
	for(int i = 0; i < array1.length; i++) {
		res = res+array1[i]*array2[i];
	}
	return res;
}

	
}
